package com.tanhua.server.server;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.autoconfig.templates.ApiFaceTemplate;
import com.tanhua.autoconfig.templates.HuanXinTemplate;
import com.tanhua.autoconfig.templates.OssTemplate;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.checkerframework.checker.units.qual.A;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private ApiFaceTemplate apiFaceTemplate;
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Autowired
    private MqMessageService mqMessageService;
    public Map loginVerificaion(String phone, String code) {
        String code1 = redisTemplate.opsForValue().get("phone:" + phone);
        if(!code.equals(code1)|| StringUtils.isEmpty(code1)){
            //不一致 抛出异常
           // ErrorResult vo = ErrorResult.loginError();
            throw new BusinessException(ErrorResult.loginError());
        }
        //清空redis
        redisTemplate.delete("phone:" + phone);
        User user = userApi.fingByMobile(phone);
        Boolean isNew = false;
//        操作类型, 0101为登录，0201为发动态，0202为浏览动态，0203为动态点赞，0204为动态喜欢，0205为评论，
//        0206为动态取消点赞，，0207为动态取消喜欢，
//        0301为发小视频，0302为小视频点赞，0303为小视频取消点赞，0304为小视频评论
        String type = "0101";
        if(user==null){
            user = new User();
            user.setPassword("123456");
            user.setMobile(phone);
            //消费者以序列化二进制传到提供者，提供者反序列化接收，两边的user根本不是同一个对象
            //所以不会自动接收plus传给user的id，此处的user的id是空的，所以接收id返回值，再设置进去
            Long id = userApi.save(user);
            user.setId(id);
            isNew=true;
            //将用户信息注册到环信
            huanXinTemplate.createUser("hx"+user.getId(),"123456");
            //更新用户的环信账号和密码到数据库
            user.setHxUser("hx"+user.getId());
            user.setHxPassword("123456");
            userApi.update(user);
            type = "0102";
        }
        //发送消息到MQ，
        mqMessageService.sendLogMessage(user.getId(),type,"log.user",null);

        Map tokenMap = new HashMap();
        tokenMap.put("id",user.getId());
        tokenMap.put("mobile",user.getMobile());
        String token = JwtUtils.getToken(tokenMap);
        HashMap remap = new HashMap<>();
        remap.put("token",token);
        remap.put("isNew",isNew);
        return remap;
    }

    public void saveUserInfo(UserInfo userInfo) {
        userInfoApi.saveMsg(userInfo);
    }

    public void head(Long id, MultipartFile headPhoto) throws IOException {
        //从文件里获取文件输入流和文件原始名称
        String url = ossTemplate.uploadFile(headPhoto.getInputStream(), headPhoto.getOriginalFilename());

        Boolean detect = apiFaceTemplate.detect(url);
        if(!detect){
            boolean delpic = ossTemplate.delpic(url);
            throw new BusinessException(ErrorResult.faceError());

        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setAvatar(url);
        userInfoApi.head(userInfo);
    }


}
